package io.github.yotta.export.web.iface;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import io.github.yotta.export.web.model.ExportTaskResult;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author yotta
 * @date 2025/4/27
 */
@Slf4j
public class InMemoryExportTaskCache implements ExportTaskCache {

    private final Cache<String, ExportTaskResult> cache;

    public InMemoryExportTaskCache() {
        this.cache = CacheBuilder.newBuilder()
                .expireAfterWrite(2, TimeUnit.HOURS)
                .build();
    }

    @Override
    public void add(String taskID, ExportTaskResult result) {
        cache.put(taskID, result);
    }

    @Override
    public ExportTaskResult get(String taskID) {
        return cache.getIfPresent(taskID);
    }
}
